package com.weidong.mybbs.interceptor;

import com.weidong.mybbs.service.AuthService;
import com.weidong.mybbs.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 临时会话拦截器
 */
@Component
public class TmpSessionInterceptor implements HandlerInterceptor {

    @Autowired
    private AuthService authService;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String sessionId = authService.getSessionId(request);
        if (StringUtils.isBlank(sessionId)){
            //生成临时sessionID
            response.addCookie(authService.generateSession());
        }
        return true;
    }
}
